package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"rpc/appconfig"
)

var DB *gorm.DB

func GetMysql() {
	// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
	var err error
	data := appconfig.AppConf.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", data.User, data.Password, data.Host, data.Port, data.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("mysql连接失败")
	}
	fmt.Println("mysql连接成功")

	// Get generic database object sql.DB to use its functions
	sqlDB, err := DB.DB()

	// SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
	sqlDB.SetMaxIdleConns(10)

	// SetMaxOpenConns sets the maximum number of open connections to the database.
	sqlDB.SetMaxOpenConns(100)

	DB.AutoMigrate(&House{})
}
